// pages/addComment/addComment.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    flag: 5,
    oid: 0,
    type: 0,
    baseUrl: getApp().globalData.baseUrl,
    showImg1: "/images/tb03.png",
    showImg2: "/images/tb03.png",
    showImg3: "/images/tb03.png",
    loadImg1: "",
    loadImg2: "",
    loadImg3: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var oid = options.id;
    var type = options.type;
    that.setData({
      oid: oid,
      type: type
    })
    // console.log(options)
  },
  chooseStart: function (e) {
    var start = e.currentTarget.dataset.start;
    this.setData({
      flag: start
    })
  },
  chooseOne: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var baseUrl = that.data.baseUrl;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        var tempFilePaths = res.tempFilePaths[0];
        wx.uploadFile({
          filePath: tempFilePaths,
          name: 'file',
          url: baseUrl + 'Index/UploadImg',
          success: function (res) {
            var data = res.data;
            var newData = JSON.parse(data).data;
            if (index == 1) {
              that.setData({
                showImg1: tempFilePaths,
                loadImg1: newData
              })
            } else if (index == 2) {
              that.setData({
                showImg2: tempFilePaths,
                loadImg2: newData
              })
            } else if (index == 3) {
              that.setData({
                showImg3: tempFilePaths,
                loadImg3: newData
              })
            }
            console.log(newData)
          }
        })

      }
    })
  },
  formSubmit: function (e) {
    var that = this;
    var content = e.detail.value.content;
    if(!content){
      wx.showToast({
        title: '请填写评论内容',
        icon:"none",
        duration:1500
      })
      return false;
    }
    var start =that.data.flag;
    var loadImg1=that.data.loadImg1;
    var loadImg2 =that.data.loadImg2;
    var loadImg3 =that.data.loadImg3;
    var type=that.data.type;
    var oid=that.data.oid;
    var uid=wx.getStorageSync('uid');
    var param = {
      'start':start,
      'content':content,
      'picture_one':loadImg1,
      "picture_two":loadImg2,
      "picture_thr":loadImg3,
      'type':type,
      'oid':oid,
      'uid':uid
    };
    app.wxRequest('POST', "Index/addCommentContent", param, (res) => {
      var code = res.code;
      if(code == 1){
        wx.showToast({
          title: res.msg,
          mask:true,
          icon:"none",
          duration:1500
        })
        setTimeout(function(){
          wx.reLaunch({
            url: '../index/index',
          })
        },1500)
      }else{
        wx.showToast({
          title: res.msg,
          icon:"none",
          duration:1500
        })
      }
    }, (err) => {
      console.log(err.errMsg)
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  }
})